<?php

class mdl_comments extends ujn_model {
	
	function find($id, $siteid = 1, $offset = 0, $max = 10) {
		$sql =<<<EOT
select `username`, from_unixtime(`creat_at`, '%Y-%m-%d') creat_at, `userid`, `content`, `id`, `support` from `v9_comment_data_{$siteid}`
		where `commentid` = 'content_{$id}'
		order by creat_at desc
		limit {$offset}, {$max};
EOT;
		return $this->mysql()->find_all($sql);
	}
	
	function append($id, $userid, $username, $content, $siteid = 1) {
		$ip = $_SERVER['REMOTE_ADDR'];
		$sql =<<<EOT
insert into `v9_comment_data_{$siteid}`
	set `commentid` = 'content_{$id}',
		`siteid` = '{$siteid}',
		`userid` = '{$userid}',
		`username` = '{$username}',
		`creat_at` = unix_timestamp(),
		`ip` = '{$ip}',
		`status` = '1',
		`content` = '{$content}',
		`direction` = '0',
		`support` = '0',
		`reply` = '0'
EOT;
		return $this->mysql()->insert($sql);
	}
	
	function find_count($id, $siteid = 1) {
		$sql =<<<EOT
select count(1) c from `v9_comment_data_{$siteid}`
		where `commentid` = 'content_{$id}';
EOT;
		$d = $this->mysql()->find_one($sql);
		return isset($d['c']) ? (int)$d['c'] : 0;
	}

	function support($id, $siteid = 1) {
		$sql =<<<EOT
update v9_comment_data_{$siteid} set support = support + 1
		where `id` = '{$id}';
EOT;
		return $this->mysql()->update($sql);
	}
	
  /**
  * @return mdl_comments
  */
  static function get_instance($empty = '') {
    return parent::get_instance(__CLASS__);
  }
}